<title>附件管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>应用</cite></a>
        <a><cite>附件管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-attachment-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="user_id" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">文件名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="file_name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">文件类型</label>
                    <div class="layui-input-block">
                        <select name="file_type">
                            <option value="">请选择</option>
                            <option value="image">图片</option>
                            <option value="video">视频</option>
                            <option value="file">文件</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">文件路径</label>
                    <div class="layui-input-inline">
                        <input type="text" name="file_path" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-attachment-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <table id="LAY-app-attachment-list" lay-filter="LAY-app-attachment-list"></table>
            <script type="text/html" id="imgTpl">
                {{# if(d.file_type === 'image'){ }}
                <img style="display: inline-block; height: 32px;" src="/storage/{{ d.file_path }}"
                     layadmin-event="image_preview">
                {{# } }}
            </script>
            <script type="text/html" id="table-file-list">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use('attachment', layui.factory('attachment')).use(['admin', 'attachment', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        form.render(null, 'app-attachment-list');

        //监听搜索
        form.on('submit(LAY-app-attachment-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-app-attachment-list', {
                where: field
            });
        });

        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-app-file-list')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.confirm('确定删除吗？', function (index) {

                    //执行 Ajax 后重载
                    /*
                    admin.req({
                      url: 'xxx'
                      //,……
                    });
                    */
                    table.reload('LAY-app-file-list');
                    layer.msg('已删除');
                });
            },
            add: function () {
                admin.popup({
                    title: '添加附件'
                    , area: ['800px', '550px']
                    , id: 'LAY-app-attachment-add'
                    , success: function (layero, index) {
                        view(this.id).render('app/attachment/listform').done(function () {
                            form.render(null, 'LAY-app-attachment-form');

                            //监听提交
                            form.on('submit(LAY-app-attachment-submit)', function (data) {
                                admin.req({
                                    url: layui.setter.host + '/attachment'
                                    , data: data.field
                                    , type: "post"
                                    , done: function (res) {
                                        table.reload('LAY-app-attachment-list'); //重载表格
                                        layer.close(index); //执行关闭
                                    }
                                });
                            });
                        });
                    }
                });
            },
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
